<template>
  <div class="app-table">
    <div class="app-body">
      <div class="app-search">
        <el-form :inline="true" class="demo-form-inline" v-model="form">
          <!-- <el-form-item
            label="所在年级"
            :label-width="formLabelWidth"
            prop="gradeId"
          >
            <el-select
              clearable
              v-model="form.gradeId"
              autocomplete="off"
              @change="filterGradeId"
              placeholder="请选择所在年级"
            >
              <el-option
                v-for="gradeId in gradeIds"
                :key="gradeId"
                :value="gradeId"
              ></el-option>
            </el-select>
          </el-form-item> -->

          <el-form-item
            label="所在学年"
            :label-width="formLabelWidth"
            prop="classId"
          >
            <el-select
              clearable
              v-model="form.schoolYear"
              autocomplete="off"
              placeholder="请选择所在学年"
            >
              <el-option
                v-for="item in schoolYears"
                :key="item"
                :value="item"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item
            label="考试名称"
            :label-width="formLabelWidth"
            prop="classId"
          >
            <el-select
              clearable
              v-model="form.typeName"
              autocomplete="off"
              placeholder="请选择考试名称"
            >
              <el-option
                v-for="item in types"
                :key="item.name"
                :value="item.name"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              plain
              @click="onSubmit"
              @keyup.enter="searchEnter"
              >搜索</el-button
            >
          </el-form-item>
        </el-form>
      </div>
      <el-table
        size="small"
        :data="list"
        border
        :row-class-name="tableRowClassName"
      >
        <el-table-column prop="index" label="序号" width="50">
          <template #default="scope">
            {{ scope.$index + 1 }}
          </template>
        </el-table-column>
        <!-- <el-table-column prop="schoolYear" label="学年" width="100"/> -->
        <el-table-column prop="name" label="考试名称" width="200" />
        <el-table-column label="操作">
          <template style="display: block" #default="scope">
            <el-button
              size="small"
              type="success"
              @click="editbaseAchievement(scope.row)"
              >一键成绩分析
            </el-button>
            <!-- <el-button
              size="small"
              type="warning"
              @click="calcFractionalLine(scope.row)"
              >计算分数线
            </el-button> -->
          </template>
        </el-table-column>
        <template v-slot:empty>
          <el-empty description="暂无数据"></el-empty>
        </template>
      </el-table>
      <el-config-provider :locale="locale">
        <el-pagination
          :total="total"
          :page-size="limit"
          :page-sizes="[5, 10, 20, 30, 40, 50, 100]"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="changePageSize"
          @current-change="changeCurrentPage"
        >
        </el-pagination>
      </el-config-provider>
    </div>
    <bottomView></bottomView>
  </div>
</template>
<script>
import bottomView from "@/components/bottom.vue";
import {
  ElConfigProvider,
  ElMessageBox,
  ElMessage,
  ElLoading,
} from "element-plus";
import zhCn from "element-plus/lib/locale/lang/zh-cn";
import { httpService } from "@/utils/httpService";
import { Picture as IconPicture, Upload } from "@element-plus/icons-vue";

export default {
  name: "baseAchievementListView",
  data() {
    return {
      type: 0,
      title: "",
      gradeIds: [],
      types: [],
      schoolYears: [],
      page: 0,
      pageSize: 10,
      total: 0,
      limit: 10,
      fullscreenLoading: false,
      list: [],
      map: {},
      classInfos: [],
      form: {
        studentNumber: "",
        classId: "",
        gradeId: "",
        password: "",
        studentName: "",
        examineNumber: "",
        idCard: "",
        fatherMobile: "",
        motherMobile: "",
        fatherName: "",
        motherName: "",
        address: "",
        fatherWorkUnit: "",
        motherWorkUnit: "",
        mobile: "",
        alias: "",
        photo: "",
        politicalOutlook: "",
        incomeDate: "",
        nation: "",
        graduateSchool: "",
        admissionScore: "",
        type: "",
        incomeCode: "",
        beforeName: "",
        nationality: "",
        modeStudy: "",
        oneCardNumber: "",
        schoolYear: "",
        typeName: "",
      },
    };
  },
  components: {
    ElLoading,
    bottomView,
    IconPicture,
    ElConfigProvider,
    ElMessageBox,
    ElMessage,
    Upload,
  },
  setup() {
    return {
      locale: zhCn,
    };
  },
  mounted() {
    this.getGradeIds();
    this.getBaseClassInfoList();
    this.getbaseAchievementList();
  },
  watch: {
    $route() {
      this.getGradeIds();
      this.getBaseClassInfoList();
      this.getbaseAchievementList();
    },
  },
  methods: {
    getBaseClassInfoList() {
      httpService("/javaApi/baseClassInfo/getAllEffectClass", {}, "get").then(
        (data) => {
          this.map = data;
        }
      );
    },
    filterGradeId(val) {
      this.classInfos = this.map[val];
    },
    getGradeIds() {
      let date = new Date();
      let nowYear = parseInt(date.getFullYear());
      let nowMonth = parseInt(date.getMonth());
      let gradeIds = [];
      if (nowMonth > 6) {
        gradeIds.push(nowYear - 2);
        gradeIds.push(nowYear - 1);
        gradeIds.push(nowYear);
      } else {
        gradeIds.push(nowYear - 3);
        gradeIds.push(nowYear - 2);
        gradeIds.push(nowYear - 1);
      }
      this.gradeIds = gradeIds;
      // this.form.gradeId = gradeIds[0]
      this.getSchoolYears();
      this.getAchievementTypeList();
    },
    getAchievementTypeList() {
      httpService("/javaApi/achievementType/list/1/1000", {}, "get").then(
        (data) => {
          this.types = data.list;
        }
      );
    },
    getSchoolYears() {
      let date = new Date();
      let nowYear = parseInt(date.getFullYear());
      let nowMonth = parseInt(date.getMonth());
      let schoolYears = [];
      if (nowMonth > 6) {
        // 2023
        schoolYears.push(nowYear - 2000 + "-" + (nowYear - 1999) + "学年上");

        schoolYears.push(nowYear - 2001 + "-" + (nowYear - 2000) + "学年下");
        schoolYears.push(nowYear - 2001 + "-" + (nowYear - 2000) + "学年上");

        schoolYears.push(nowYear - 2002 + "-" + (nowYear - 2001) + "学年下");
        schoolYears.push(nowYear - 2002 + "-" + (nowYear - 2001) + "学年上");
      } else {
        schoolYears.push(nowYear - 2001 + "-" + (nowYear - 2000) + "学年下");
        schoolYears.push(nowYear - 2001 + "-" + (nowYear - 2000) + "学年上");

        schoolYears.push(nowYear - 2002 + "-" + (nowYear - 2001) + "学年下");
        schoolYears.push(nowYear - 2002 + "-" + (nowYear - 2001) + "学年上");

        schoolYears.push(nowYear - 2003 + "-" + (nowYear - 2002) + "学年下");
        schoolYears.push(nowYear - 2003 + "-" + (nowYear - 2002) + "学年上");
      }
      this.schoolYears = schoolYears;
      // this.form.schoolYear = schoolYears[0]
    },
    searchEnter: function (e) {

      // 使用 which 和 keyCode 属性来解决兼容问题
      var keyCode = window.event ? e.keyCode : e.which;
      var val = e.target.value;
      if (keyCode == 13 && val) {

        this.getbaseAchievementList();
      }
    },
    add() {
      this.$refs.addEditBaseAchievement.title = "添加学生";
      this.$refs.addEditBaseAchievement.dialogFormVisible = true;
    },
 
    calcRepeat() {
      if (this.form.gradeId == "") {
        ElMessage.error("请选择年级后计算");
        return;
      }
      if (this.form.schoolYear == "") {
        ElMessage.error("请选择学年后计算");
        return;
      }
      if (this.form.typeName == "") {
        ElMessage.error("请选择考试名称后计算");
        return;
      }

      ElMessageBox.confirm("确定要重算排名吗？", "提醒", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          let params = new FormData(); //创建一个form对象,必须是form对象否则后端接受不到数据
          params.append("typeName", this.form.typeName);
          params.append("schoolYear", this.form.schoolYear);
          httpService(
            "/javaApi/baseAchievement/calcRepeat",
            params,
            "post"
          ).then((data) => {
            if (data.code == 200) {
              ElMessage.success(data.message);
              this.getbaseAchievementList();
            }
          });
        })
        .catch(() => {});
    },
    editbaseAchievement(obj) {
      ElMessageBox.confirm("确定要执行成绩分析吗？", "提醒", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          httpService(
            "/javaApi/baseAchievementAnalysis/queryClassFinishPercentage",
            "name=" + obj.name,
            "post"
          ).then((data) => {
            if (data.code == 200) {
              ElMessage.success("执行成功");
            } else {
              ElMessage.error("执行失败，请重试！");
            }
          });
        })
        .catch(() => {});
    },
    reloadData() {
      this.page = 1;
      this.getbaseAchievementList();
    },

    onSubmit() {
      this.getbaseAchievementList();
    },
    // 每页记录数改变，size：回调参数，表示当前选中的“每页条数”
    changePageSize(size) {
      this.pageSize = size;
      this.limit = size;
      this.getbaseAchievementList();
    },

    // 改变页码，page：回调参数，表示当前选中的“页码”
    changeCurrentPage(page) {
      this.page = page;
      this.getbaseAchievementList();
    },
    tableRowClassName({ row, rowIndex }) {
      return "";
    },
    
    getbaseAchievementList() {
      httpService(
        "/javaApi/baseAchievement/listGroup/" + this.page + "/" + this.pageSize,
        {
          typeName: this.form.typeName,
          schoolYear: this.form.schoolYear,
        },
        "get"
      ).then((data) => {
        this.list = data.list;
        this.total = data.total;
        this.pageCount = data.pages;
      });
    },
  },
};
</script>
<style scoped>
.score {
  width: 30px;
  text-align: center;
  display: inline-block;
}
.el-main {
  padding: 0px;
}

.teacher-content {
  width: 100%;
}

.app-table {
  background: #eff3f5;
  padding: 10px;
  width: 100%;
}

.app-search {
}

.app-separator {
  padding-top: 10px;
  padding-bottom: 20px;
  padding-left: 10px;
}

.app-body {
  background: white;
  padding: 20px 20px 10px 20px;
  margin-bottom: 10px;
  min-height: calc(100vh - 204px);
}

.el-table .warning-row {
  --el-table-tr-bg-color: var(--el-color-warning-lighter);
}

.el-table .success-row {
  --el-table-tr-bg-color: var(--el-color-success-lighter);
}

.el-pagination {
  text-align: center;
  margin-top: 10px;
}
</style>

